package me.jessyan.mvparms.demo.mvp.ui.activity;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.Button;
import android.widget.Toast;

import com.jess.arms.base.BaseActivity;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.utils.ArmsUtils;
import com.lzy.imagepicker.ImagePicker;
import com.lzy.imagepicker.bean.ImageItem;
import com.lzy.imagepicker.ui.ImageGridActivity;
import com.tbruyelle.rxpermissions2.RxPermissions;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.BindView;
import butterknife.OnClick;
import me.jessyan.mvparms.demo.R;
import me.jessyan.mvparms.demo.data.entity.User;
import me.jessyan.mvparms.demo.di.component.DaggerMainComponent;
import me.jessyan.mvparms.demo.di.module.MainModule;
import me.jessyan.mvparms.demo.mvp.contract.MainContract;
import me.jessyan.mvparms.demo.mvp.presenter.MainPresenter;
import me.jessyan.mvparms.demo.mvp.ui.adapter.MessageListAdapter;
import me.jessyan.mvparms.demo.richeditor.RichEditorActivity;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MainActivity extends BaseActivity<MainPresenter> implements MainContract.View {

    private static final int REQUEST_CODE_CHOOSE = 0;

    @BindView(R.id.btn_start_rich_text_editor)
    Button mBtnStartRichTextEditor;


    @Inject
    List<User> mUserList;
    @BindView(R.id.btn_upload_image)
    Button mBtnUploadImage;
    @BindView(R.id.btn_show_message_list)
    Button mBtnShowMessageList;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public void setupActivityComponent(@NonNull AppComponent appComponent) {
        DaggerMainComponent
                .builder()
                .appComponent(appComponent)
                .mainModule(new MainModule(this))
                .build()
                .inject(this);
    }

    @Override
    public int initView(@Nullable Bundle savedInstanceState) {
        return R.layout.activity_main;
    }

    @Override
    public void initData(@Nullable Bundle savedInstanceState) {

    }

    @Override
    public Activity getActivity() {
        return this;
    }

    @Override
    public RxPermissions getRxPermissions() {
        return null;
    }

    @Override
    public void showLoading(String message) {
        Toast.makeText(this, message, Toast.LENGTH_SHORT).show();

    }

    @Override
    public void hideLoading() {
    }

    @Override
    public void showMessage(@NonNull String message) {
        ArmsUtils.makeText(this, message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {
        finish();
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == ImagePicker.RESULT_CODE_ITEMS
                && data != null
                && requestCode == REQUEST_CODE_CHOOSE) {
            ArrayList<ImageItem> images =
                    (ArrayList<ImageItem>) data.getSerializableExtra(ImagePicker.EXTRA_RESULT_ITEMS);
            if (images != null && !images.isEmpty()) {

                ImageItem imageItem = images.get(0);
                File imageFile = new File(imageItem.path);
                RequestBody photoRequestBody = RequestBody.create(MediaType.parse("image/jpg"), imageFile);
                MultipartBody.Part photoPart = MultipartBody.Part.createFormData("file", imageFile.getName(), photoRequestBody);
//                RequestBody requestBody = RequestBody.create(MediaType.parse("multipart/form-data"), imageFile);
//                MultipartBody.Part body = MultipartBody.Part.createFormData("image", imageFile.getName(), requestBody);
                mPresenter.uploadImage(photoPart);
            }
        }
    }

    @OnClick({R.id.btn_start_rich_text_editor, R.id.btn_upload_image})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.btn_start_rich_text_editor:
                launchActivity(new Intent(this, RichEditorActivity.class));
                break;

            case R.id.btn_upload_image:
                Intent intent = new Intent(this, ImageGridActivity.class);
                startActivityForResult(intent, REQUEST_CODE_CHOOSE);
                break;

            default:
                break;
        }
    }

    @OnClick(R.id.btn_show_message_list)
    public void showMessageList() {
        launchActivity(new Intent(this, MessageListActivity.class));
    }
}
